<!DOCTYPE html>
<html lang="zh-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Docker 网络 | 静思田园</title>
    <meta name="description" content="时不时记录一点点">
    <meta name="generator" content="VitePress v1.5.0">
    <link rel="preload stylesheet" href="/assets/style.D1KHeDuB.css" as="style">
    <link rel="preload stylesheet" href="/vp-icons.css" as="style">
    <script type="module" src="/assets/chunks/metadata.d4b76d32.js"></script>
    <script type="module" src="/assets/app.Cqoht_Of.js"></script>
    <link rel="preload" href="/assets/inter-roman-latin.Di8DUHzh.woff2" as="font" type="font/woff2" crossorigin="">
    <link rel="modulepreload" href="/assets/chunks/framework.trnLO_3_.js">
    <link rel="modulepreload" href="/assets/chunks/theme.BNMYWj-o.js">
    <link rel="modulepreload" href="/assets/chunks/katex.BZy9Y_85.js">
    <link rel="modulepreload" href="/assets/chunks/c4Diagram-3d4e48cf.rhyfUO3k.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-66a62f08.BvVfKYLl.js">
    <link rel="modulepreload" href="/assets/chunks/flowDiagram-v2-96b9c2cf.rYAPEXyw.js">
    <link rel="modulepreload" href="/assets/chunks/erDiagram-9861fffd.CQSbaoJM.js">
    <link rel="modulepreload" href="/assets/chunks/gitGraphDiagram-72cf32ee.CjLmC8RV.js">
    <link rel="modulepreload" href="/assets/chunks/ganttDiagram-c361ad54.R_V-FeK5.js">
    <link rel="modulepreload" href="/assets/chunks/infoDiagram-f8f76790.C-2Va9KZ.js">
    <link rel="modulepreload" href="/assets/chunks/pieDiagram-8a3498a8.B9ZVkD7K.js">
    <link rel="modulepreload" href="/assets/chunks/quadrantDiagram-120e2f19.BuonnDHi.js">
    <link rel="modulepreload" href="/assets/chunks/xychartDiagram-e933f94c.C_bIYaUn.js">
    <link rel="modulepreload" href="/assets/chunks/requirementDiagram-deff3bca.RFVzvdjl.js">
    <link rel="modulepreload" href="/assets/chunks/sequenceDiagram-704730f1.UZWxMlyq.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-70f12bd4.sipmPMFa.js">
    <link rel="modulepreload" href="/assets/chunks/classDiagram-v2-f2320105.CDkLxQ0T.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-587899a1.CD2ocVZB.js">
    <link rel="modulepreload" href="/assets/chunks/stateDiagram-v2-d93cdb3a.D0qUPm3J.js">
    <link rel="modulepreload" href="/assets/chunks/journeyDiagram-49397b02.CeUUZA2l.js">
    <link rel="modulepreload" href="/assets/chunks/flowchart-elk-definition-4a651766.DkdgpMTf.js">
    <link rel="modulepreload" href="/assets/chunks/timeline-definition-85554ec2.WL9yxus9.js">
    <link rel="modulepreload" href="/assets/chunks/mindmap-definition-fc14e90a.CWC2bxmB.js">
    <link rel="modulepreload" href="/assets/chunks/sankeyDiagram-04a897e0.BXj8OViR.js">
    <link rel="modulepreload" href="/assets/chunks/blockDiagram-38ab4fdb.CPVnJXk5.js">
    <link rel="modulepreload" href="/assets/chunks/virtual_mermaid-config.DDnGl6nM.js">
    <link rel="modulepreload" href="/assets/docker_network.md.aDCdhwdD.lean.js">
    <meta name="application-name" content="VuePress">
    <meta name="apple-mobile-web-app-title" content="静思田园">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileColor" content="#3eaf7c">
    <meta name="theme-color" content="#646cff">
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <script id="check-dark-mode">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"auto",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
    <script id="check-mac-os">document.documentElement.classList.toggle("mac",/Mac|iPhone|iPod|iPad/i.test(navigator.platform));</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-5d98c3a5><!--[--><!--]--><!--[--><span tabindex="-1" data-v-0f60ec36></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-0f60ec36> Skip to content </a><!--]--><!----><header class="VPNav" data-v-5d98c3a5 data-v-ae24b3ad><div class="VPNavBar" data-v-ae24b3ad data-v-6aa21345><div class="wrapper" data-v-6aa21345><div class="container" data-v-6aa21345><div class="title" data-v-6aa21345><div class="VPNavBarTitle has-sidebar" data-v-6aa21345 data-v-1168a8e4><a class="title" href="/" data-v-1168a8e4><!--[--><!--]--><!--[--><img class="VPImage logo" src="/logo.png" alt data-v-8426fc1a><!--]--><span data-v-1168a8e4>静思田园</span><!--[--><!--]--></a></div></div><div class="content" data-v-6aa21345><div class="content-body" data-v-6aa21345><!--[--><!--]--><div class="VPNavBarSearch search" data-v-6aa21345><!--[--><!----><div id="local-search"><button type="button" class="DocSearch DocSearch-Button" aria-label="搜索文档"><span class="DocSearch-Button-Container"><span class="vp-icon DocSearch-Search-Icon"></span><span class="DocSearch-Button-Placeholder">搜索文档</span></span><span class="DocSearch-Button-Keys"><kbd class="DocSearch-Button-Key"></kbd><kbd class="DocSearch-Button-Key">K</kbd></span></button></div><!--]--></div><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-6aa21345 data-v-dc692963><span id="main-nav-aria-label" class="visually-hidden" data-v-dc692963> Main Navigation </span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57></span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/network/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>网络工程师</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/frontend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>前端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/English/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>English</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Scrupy/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>爬虫</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/backend/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>后端</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/bigdata/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>大数据</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/computer/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>计算机基础</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/database/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>数据库</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/docker/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>dorker</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/git/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Git</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/nodejs/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Node.js</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/python/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>Python</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/shell/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>shell</span><!--]--></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/tools/" tabindex="0" data-v-dc692963 data-v-e56f3d57><!--[--><span data-v-e56f3d57>工具</span><!--]--></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-6aa21345 data-v-6c893767><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-6c893767 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-6aa21345 data-v-0394ad82 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-6aa21345 data-v-bb2aa2f0 data-v-cf11d7a2><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-cf11d7a2><span class="vpi-more-horizontal icon" data-v-cf11d7a2></span></button><div class="menu" data-v-cf11d7a2><div class="VPMenu" data-v-cf11d7a2 data-v-b98bc113><!----><!--[--><!--[--><!----><div class="group" data-v-bb2aa2f0><div class="item appearance" data-v-bb2aa2f0><p class="label" data-v-bb2aa2f0>Appearance</p><div class="appearance-action" data-v-bb2aa2f0><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" title aria-checked="false" data-v-bb2aa2f0 data-v-5337faa4 data-v-1d5665e3><span class="check" data-v-1d5665e3><span class="icon" data-v-1d5665e3><!--[--><span class="vpi-sun sun" data-v-5337faa4></span><span class="vpi-moon moon" data-v-5337faa4></span><!--]--></span></span></button></div></div></div><div class="group" data-v-bb2aa2f0><div class="item social-links" data-v-bb2aa2f0><div class="VPSocialLinks social-links-list" data-v-bb2aa2f0 data-v-7bc22406><!--[--><a class="VPSocialLink no-icon" href="https://github.com/loveagri" aria-label="github" target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><span class="vpi-social-github"></span></a><a class="VPSocialLink no-icon" href="https://gitee.com/loveagri" aria-label target="_blank" rel="noopener" data-v-7bc22406 data-v-bd121fe5><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>码云</title><path d="M11.984 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.016 0zm6.09 5.333c.328 0 .593.266.592.593v1.482a.594.594 0 0 1-.593.592H9.777c-.982 0-1.778.796-1.778 1.778v5.63c0 .327.266.592.593.592h5.63c.982 0 1.778-.796 1.778-1.778v-.296a.593.593 0 0 0-.592-.593h-4.15a.592.592 0 0 1-.592-.592v-1.482a.593.593 0 0 1 .593-.592h6.815c.327 0 .593.265.593.592v3.408a4 4 0 0 1-4 4H5.926a.593.593 0 0 1-.593-.593V9.778a4.444 4.444 0 0 1 4.445-4.444h8.296Z"/></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-6aa21345 data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><div class="divider" data-v-6aa21345><div class="divider-line" data-v-6aa21345></div></div></div><!----></header><div class="VPLocalNav has-sidebar empty" data-v-5d98c3a5 data-v-a6f0e41e><div class="container" data-v-a6f0e41e><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-a6f0e41e><span class="vpi-align-left menu-icon" data-v-a6f0e41e></span><span class="menu-text" data-v-a6f0e41e>Menu</span></button><div class="VPLocalNavOutlineDropdown" style="--vp-vh:0px;" data-v-a6f0e41e data-v-17a5e62e><button data-v-17a5e62e>Return to top</button><!----></div></div></div><aside class="VPSidebar" data-v-5d98c3a5 data-v-319d5ca6><div class="curtain" data-v-319d5ca6></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-319d5ca6><span class="visually-hidden" id="sidebar-aria-label" data-v-319d5ca6> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="no-transition group" data-v-c40bc020><section class="VPSidebarItem level-0 has-active" data-v-c40bc020 data-v-b7550ba0><div class="item" role="button" tabindex="0" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><h2 class="text" data-v-b7550ba0>dorker</h2><!----></div><div class="items" data-v-b7550ba0><!--[--><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>dorker</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/Dockerfile.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Dockerfile</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/compose.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker-compose</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/concept.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker 基础概念</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/container.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>container</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/images.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>images</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/mac-docker-auto.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Mac下给docker配置命令补全</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/multi-architecture.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker 的多架构支持</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/network.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Docker 网络</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/security.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>安全</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/storate.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>存储Volume &amp; Bind mount</p><!--]--></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-b7550ba0 data-v-b7550ba0><div class="item" data-v-b7550ba0><div class="indicator" data-v-b7550ba0></div><a class="VPLink link link" href="/docker/swarm.html" data-v-b7550ba0><!--[--><p class="text" data-v-b7550ba0>Swarm</p><!--]--></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-5d98c3a5 data-v-1428d186><div class="VPDoc has-sidebar has-aside" data-v-1428d186 data-v-39a288b8><!--[--><!--]--><div class="container" data-v-39a288b8><div class="aside" data-v-39a288b8><div class="aside-curtain" data-v-39a288b8></div><div class="aside-container" data-v-39a288b8><div class="aside-content" data-v-39a288b8><div class="VPDocAside" data-v-39a288b8 data-v-3f215769><!--[--><!--]--><!--[--><!--]--><nav aria-labelledby="doc-outline-aria-label" class="VPDocAsideOutline" data-v-3f215769 data-v-a5bbad30><div class="content" data-v-a5bbad30><div class="outline-marker" data-v-a5bbad30></div><div aria-level="2" class="outline-title" id="doc-outline-aria-label" role="heading" data-v-a5bbad30>目录</div><ul class="VPDocOutlineItem root" data-v-a5bbad30 data-v-b933a997><!--[--><!--]--></ul></div></nav><!--[--><!--]--><div class="spacer" data-v-3f215769></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-39a288b8><div class="content-container" data-v-39a288b8><!--[--><!--]--><main class="main" data-v-39a288b8><div style="position:relative;" class="vp-doc _docker_network" data-v-39a288b8><div><h1 id="docker-网络" tabindex="-1">Docker 网络 <a class="header-anchor" href="#docker-网络" aria-label="Permalink to &quot;Docker 网络&quot;">​</a></h1><h2 id="docker-bridge-网络" tabindex="-1">Docker Bridge 网络 <a class="header-anchor" href="#docker-bridge-网络" aria-label="Permalink to &quot;Docker Bridge 网络&quot;">​</a></h2><h3 id="创建容器" tabindex="-1">创建容器 <a class="header-anchor" href="#创建容器" aria-label="Permalink to &quot;创建容器&quot;">​</a></h3><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> run</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --rm</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> box1</span><span style="color:#98C379;"> busybox</span><span style="color:#98C379;"> /bin/sh</span><span style="color:#D19A66;"> -c</span><span style="color:#98C379;"> &quot;while true; do sleep 3600; done&quot;</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> run</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --rm</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> box2</span><span style="color:#98C379;"> busybox</span><span style="color:#98C379;"> /bin/sh</span><span style="color:#D19A66;"> -c</span><span style="color:#98C379;"> &quot;while true; do sleep 3600; done&quot;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="创建docker网络" tabindex="-1">创建docker网络 <a class="header-anchor" href="#创建docker网络" aria-label="Permalink to &quot;创建docker网络&quot;">​</a></h3><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> create</span><span style="color:#D19A66;"> -d</span><span style="color:#98C379;"> bridge</span><span style="color:#98C379;"> mybridge</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> ls</span><span style="color:#ABB2BF;">                       </span></span>
<span class="line"><span style="color:#61AFEF;">NETWORK</span><span style="color:#98C379;"> ID</span><span style="color:#98C379;">     NAME</span><span style="color:#98C379;">       DRIVER</span><span style="color:#98C379;">    SCOPE</span></span>
<span class="line"><span style="color:#61AFEF;">3d429aefb28d</span><span style="color:#98C379;">   bridge</span><span style="color:#98C379;">     bridge</span><span style="color:#98C379;">    local</span></span>
<span class="line"><span style="color:#61AFEF;">4b38069e4550</span><span style="color:#98C379;">   host</span><span style="color:#98C379;">       host</span><span style="color:#98C379;">      local</span></span>
<span class="line"><span style="color:#61AFEF;">bafb6e4b0700</span><span style="color:#98C379;">   mybridge</span><span style="color:#98C379;">   bridge</span><span style="color:#98C379;">    local</span></span>
<span class="line"><span style="color:#61AFEF;">f97986836031</span><span style="color:#98C379;">   none</span><span style="color:#98C379;">       null</span><span style="color:#98C379;">      local</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 创建自定义网关和子网掩码的网络</span></span>
<span class="line"><span style="color:#61AFEF;">$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> create</span><span style="color:#D19A66;"> -d</span><span style="color:#98C379;"> bridge</span><span style="color:#D19A66;"> --gateway</span><span style="color:#D19A66;"> 172.200.0.1</span><span style="color:#D19A66;"> --subnet</span><span style="color:#98C379;"> 172.200.0.0/16</span><span style="color:#98C379;"> youbridge</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 创建容器，指定连接的网络</span></span>
<span class="line"><span style="color:#61AFEF;">$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> run</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --rm</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> box3</span><span style="color:#D19A66;"> --network</span><span style="color:#98C379;"> mybridge</span><span style="color:#98C379;"> busybox</span><span style="color:#98C379;"> /bin/sh</span><span style="color:#D19A66;"> -c</span><span style="color:#98C379;"> &quot;while true; do sleep 3600; done&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 查看web3配置</span></span>
<span class="line"><span style="color:#61AFEF;">$</span><span style="color:#98C379;"> docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> inspect</span><span style="color:#98C379;"> box3</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h3 id="连接docker-bridge-网络" tabindex="-1">连接docker bridge 网络 <a class="header-anchor" href="#连接docker-bridge-网络" aria-label="Permalink to &quot;连接docker bridge 网络&quot;">​</a></h3><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> connect</span><span style="color:#98C379;"> bridge</span><span style="color:#98C379;"> box3</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><h3 id="关闭docker-bridge-网络" tabindex="-1">关闭docker bridge 网络 <a class="header-anchor" href="#关闭docker-bridge-网络" aria-label="Permalink to &quot;关闭docker bridge 网络&quot;">​</a></h3><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> disconnect</span><span style="color:#98C379;"> bridge</span><span style="color:#98C379;"> box3</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><h3 id="查看网络" tabindex="-1">查看网络 <a class="header-anchor" href="#查看网络" aria-label="Permalink to &quot;查看网络&quot;">​</a></h3><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> ls</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> network</span><span style="color:#98C379;"> inspect</span><span style="color:#98C379;"> bridge</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="tip custom-block"><p class="custom-block-title">提示</p><p><code>brctl</code> 显示Linux bridge信息，使用前需要安装, 对于CentOS, 可以通过 <code>sudo yum install -y bridge-utils</code> 安装. 对于Ubuntu, 可以通过 <code>sudo apt-get install -y bridge-utils</code></p></div><h3 id="容器对外通信" tabindex="-1">容器对外通信 <a class="header-anchor" href="#容器对外通信" aria-label="Permalink to &quot;容器对外通信&quot;">​</a></h3><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># 查看路由</span></span>
<span class="line"><span style="color:#61AFEF;">ip</span><span style="color:#98C379;"> route</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># result start</span></span>
<span class="line"><span style="color:#61AFEF;">default</span><span style="color:#98C379;"> via</span><span style="color:#D19A66;"> 10.0.2.2</span><span style="color:#98C379;"> dev</span><span style="color:#98C379;"> eth0</span><span style="color:#98C379;"> proto</span><span style="color:#98C379;"> dhcp</span><span style="color:#98C379;"> src</span><span style="color:#D19A66;"> 10.0.2.15</span><span style="color:#98C379;"> metric</span><span style="color:#D19A66;"> 100</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"><span style="color:#61AFEF;">10.0.2.0/24</span><span style="color:#98C379;"> dev</span><span style="color:#98C379;"> eth0</span><span style="color:#98C379;"> proto</span><span style="color:#98C379;"> kernel</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> link</span><span style="color:#98C379;"> src</span><span style="color:#D19A66;"> 10.0.2.15</span><span style="color:#98C379;"> metric</span><span style="color:#D19A66;"> 100</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"><span style="color:#61AFEF;">10.0.2.2</span><span style="color:#98C379;"> dev</span><span style="color:#98C379;"> eth0</span><span style="color:#98C379;"> proto</span><span style="color:#98C379;"> dhcp</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> link</span><span style="color:#98C379;"> src</span><span style="color:#D19A66;"> 10.0.2.15</span><span style="color:#98C379;"> metric</span><span style="color:#D19A66;"> 100</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"><span style="color:#61AFEF;">10.0.2.3</span><span style="color:#98C379;"> dev</span><span style="color:#98C379;"> eth0</span><span style="color:#98C379;"> proto</span><span style="color:#98C379;"> dhcp</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> link</span><span style="color:#98C379;"> src</span><span style="color:#D19A66;"> 10.0.2.15</span><span style="color:#98C379;"> metric</span><span style="color:#D19A66;"> 100</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"><span style="color:#61AFEF;">172.17.0.0/16</span><span style="color:#98C379;"> dev</span><span style="color:#98C379;"> docker0</span><span style="color:#98C379;"> proto</span><span style="color:#98C379;"> kernel</span><span style="color:#98C379;"> scope</span><span style="color:#98C379;"> link</span><span style="color:#98C379;"> src</span><span style="color:#D19A66;"> 172.17.0.1</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># result end</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># iptable 转发规则</span></span>
<span class="line"><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> iptables</span><span style="color:#D19A66;"> --list</span><span style="color:#D19A66;"> -t</span><span style="color:#98C379;"> nat</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># result start</span></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> PREROUTING</span><span style="color:#ABB2BF;"> (policy </span><span style="color:#98C379;">ACCEPT</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;">target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"><span style="color:#61AFEF;">DOCKER</span><span style="color:#98C379;">     all</span><span style="color:#D19A66;">  --</span><span style="color:#98C379;">  anywhere</span><span style="color:#98C379;">             anywhere</span><span style="color:#98C379;">             ADDRTYPE</span><span style="color:#98C379;"> match</span><span style="color:#98C379;"> dst-type</span><span style="color:#98C379;"> LOCAL</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> INPUT</span><span style="color:#ABB2BF;"> (policy </span><span style="color:#98C379;">ACCEPT</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;">target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> OUTPUT</span><span style="color:#ABB2BF;"> (policy </span><span style="color:#98C379;">ACCEPT</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;">target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"><span style="color:#61AFEF;">DOCKER</span><span style="color:#98C379;">     all</span><span style="color:#D19A66;">  --</span><span style="color:#98C379;">  anywhere</span><span style="color:#98C379;">            !localhost/8</span><span style="color:#98C379;">          ADDRTYPE</span><span style="color:#98C379;"> match</span><span style="color:#98C379;"> dst-type</span><span style="color:#98C379;"> LOCAL</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> POSTROUTING</span><span style="color:#ABB2BF;"> (policy </span><span style="color:#98C379;">ACCEPT</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;">target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"><span style="color:#61AFEF;">MASQUERADE</span><span style="color:#98C379;">  all</span><span style="color:#D19A66;">  --</span><span style="color:#98C379;">  172.17.0.0/16</span><span style="color:#98C379;">        anywhere</span><span style="color:#ABB2BF;">            </span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> DOCKER</span><span style="color:#ABB2BF;"> (2 </span><span style="color:#98C379;">references</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;">target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"><span style="color:#61AFEF;">RETURN</span><span style="color:#98C379;">     all</span><span style="color:#D19A66;">  --</span><span style="color:#98C379;">  anywhere</span><span style="color:#98C379;">             anywhere</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># result end</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><div class="warning custom-block"><p class="custom-block-title">警告</p><p>经过测试，新版docker，自定义的网络已经无法实现DNS功能，也就是说无法通过直接ping 容器的名字连接访问的主机。</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># 在docker内</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> exec</span><span style="color:#D19A66;"> -it</span><span style="color:#98C379;"> box1</span><span style="color:#98C379;"> ping</span><span style="color:#98C379;"> box2</span><span style="color:#7F848E;font-style:italic;"> #从box1 ping box2 不通</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></div><h3 id="端口转发or映射" tabindex="-1">端口转发or映射 <a class="header-anchor" href="#端口转发or映射" aria-label="Permalink to &quot;端口转发or映射&quot;">​</a></h3><p>创建容器</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#7F848E;font-style:italic;"># 创建容器</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> run</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --rm</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> web</span><span style="color:#D19A66;"> -p</span><span style="color:#98C379;"> 8080:80</span><span style="color:#98C379;"> nginx</span><span style="color:#ABB2BF;"> </span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 查看内部地址</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> inspect</span><span style="color:#D19A66;"> --format</span><span style="color:#98C379;"> &#39;{{.NetworkSettings.IPAddress}}&#39;</span><span style="color:#98C379;"> web</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># 创建另一个容器访问NGINX</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> run</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --rm</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> client</span><span style="color:#98C379;"> busybox</span><span style="color:#98C379;"> /bin/sh</span><span style="color:#D19A66;"> -c</span><span style="color:#98C379;"> &quot;while true; do sleep 3600; done&quot;</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> inspect</span><span style="color:#D19A66;"> --format</span><span style="color:#98C379;"> &#39;{{.NetworkSettings.IPAddress}}&#39;</span><span style="color:#98C379;"> client</span></span>
<span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> exec</span><span style="color:#D19A66;"> -it</span><span style="color:#98C379;"> client</span><span style="color:#98C379;"> wget</span><span style="color:#98C379;"> http://172.17.0.2</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>查看iptables的端口转发规则</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">sudo</span><span style="color:#98C379;"> iptables</span><span style="color:#D19A66;"> -t</span><span style="color:#98C379;"> nat</span><span style="color:#D19A66;"> -nvxL</span></span>
<span class="line"></span>
<span class="line"><span style="color:#7F848E;font-style:italic;"># result start</span></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> PREROUTING</span><span style="color:#ABB2BF;"> (policy </span><span style="color:#98C379;">ACCEPT</span><span style="color:#D19A66;"> 0</span><span style="color:#98C379;"> packets,</span><span style="color:#D19A66;"> 0</span><span style="color:#98C379;"> bytes</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;">    pkts</span><span style="color:#98C379;">      bytes</span><span style="color:#98C379;"> target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> in</span><span style="color:#98C379;">     out</span><span style="color:#98C379;">     source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"><span style="color:#61AFEF;">       5</span><span style="color:#D19A66;">      220</span><span style="color:#98C379;"> DOCKER</span><span style="color:#98C379;">     all</span><span style="color:#D19A66;">  --</span><span style="color:#E5C07B;">  *</span><span style="color:#E5C07B;">      *</span><span style="color:#98C379;">       0.0.0.0/0</span><span style="color:#98C379;">            0.0.0.0/0</span><span style="color:#98C379;">            ADDRTYPE</span><span style="color:#98C379;"> match</span><span style="color:#98C379;"> dst-type</span><span style="color:#98C379;"> LOCAL</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> INPUT</span><span style="color:#ABB2BF;"> (policy </span><span style="color:#98C379;">ACCEPT</span><span style="color:#D19A66;"> 0</span><span style="color:#98C379;"> packets,</span><span style="color:#D19A66;"> 0</span><span style="color:#98C379;"> bytes</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;">    pkts</span><span style="color:#98C379;">      bytes</span><span style="color:#98C379;"> target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> in</span><span style="color:#98C379;">     out</span><span style="color:#98C379;">     source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> OUTPUT</span><span style="color:#ABB2BF;"> (policy </span><span style="color:#98C379;">ACCEPT</span><span style="color:#D19A66;"> 0</span><span style="color:#98C379;"> packets,</span><span style="color:#D19A66;"> 0</span><span style="color:#98C379;"> bytes</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;">    pkts</span><span style="color:#98C379;">      bytes</span><span style="color:#98C379;"> target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> in</span><span style="color:#98C379;">     out</span><span style="color:#98C379;">     source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"><span style="color:#61AFEF;">       0</span><span style="color:#D19A66;">        0</span><span style="color:#98C379;"> DOCKER</span><span style="color:#98C379;">     all</span><span style="color:#D19A66;">  --</span><span style="color:#E5C07B;">  *</span><span style="color:#E5C07B;">      *</span><span style="color:#98C379;">       0.0.0.0/0</span><span style="color:#98C379;">           !127.0.0.0/8</span><span style="color:#98C379;">          ADDRTYPE</span><span style="color:#98C379;"> match</span><span style="color:#98C379;"> dst-type</span><span style="color:#98C379;"> LOCAL</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> POSTROUTING</span><span style="color:#ABB2BF;"> (policy </span><span style="color:#98C379;">ACCEPT</span><span style="color:#D19A66;"> 0</span><span style="color:#98C379;"> packets,</span><span style="color:#D19A66;"> 0</span><span style="color:#98C379;"> bytes</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;">    pkts</span><span style="color:#98C379;">      bytes</span><span style="color:#98C379;"> target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> in</span><span style="color:#98C379;">     out</span><span style="color:#98C379;">     source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"><span style="color:#61AFEF;">      23</span><span style="color:#D19A66;">     1532</span><span style="color:#98C379;"> MASQUERADE</span><span style="color:#98C379;">  all</span><span style="color:#D19A66;">  --</span><span style="color:#E5C07B;">  *</span><span style="color:#98C379;">      !docker0</span><span style="color:#98C379;">  172.17.0.0/16</span><span style="color:#98C379;">        0.0.0.0/0</span><span style="color:#ABB2BF;">           </span></span>
<span class="line"><span style="color:#61AFEF;">       0</span><span style="color:#D19A66;">        0</span><span style="color:#98C379;"> MASQUERADE</span><span style="color:#98C379;">  tcp</span><span style="color:#D19A66;">  --</span><span style="color:#E5C07B;">  *</span><span style="color:#E5C07B;">      *</span><span style="color:#D19A66;">       172.17.0.2</span><span style="color:#D19A66;">           172.17.0.2</span><span style="color:#98C379;">           tcp</span><span style="color:#98C379;"> dpt:80</span></span>
<span class="line"></span>
<span class="line"><span style="color:#61AFEF;">Chain</span><span style="color:#98C379;"> DOCKER</span><span style="color:#ABB2BF;"> (2 </span><span style="color:#98C379;">references</span><span style="color:#ABB2BF;">)</span></span>
<span class="line"><span style="color:#61AFEF;">    pkts</span><span style="color:#98C379;">      bytes</span><span style="color:#98C379;"> target</span><span style="color:#98C379;">     prot</span><span style="color:#98C379;"> opt</span><span style="color:#98C379;"> in</span><span style="color:#98C379;">     out</span><span style="color:#98C379;">     source</span><span style="color:#98C379;">               destination</span><span style="color:#ABB2BF;">         </span></span>
<span class="line"><span style="color:#61AFEF;">       0</span><span style="color:#D19A66;">        0</span><span style="color:#98C379;"> RETURN</span><span style="color:#98C379;">     all</span><span style="color:#D19A66;">  --</span><span style="color:#98C379;">  docker0</span><span style="color:#E5C07B;"> *</span><span style="color:#98C379;">       0.0.0.0/0</span><span style="color:#98C379;">            0.0.0.0/0</span><span style="color:#ABB2BF;">           </span></span>
<span class="line highlighted"><span style="color:#61AFEF;">       0</span><span style="color:#D19A66;">        0</span><span style="color:#98C379;"> DNAT</span><span style="color:#98C379;">       tcp</span><span style="color:#D19A66;">  --</span><span style="color:#98C379;">  !docker0</span><span style="color:#E5C07B;"> *</span><span style="color:#98C379;">       0.0.0.0/0</span><span style="color:#98C379;">            0.0.0.0/0</span><span style="color:#98C379;">            tcp</span><span style="color:#98C379;"> dpt:8080</span><span style="color:#98C379;"> to:172.17.0.2:80</span></span>
<span class="line"><span style="color:#7F848E;font-style:italic;">#result end</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h2 id="host-网络" tabindex="-1">HOST 网络 <a class="header-anchor" href="#host-网络" aria-label="Permalink to &quot;HOST 网络&quot;">​</a></h2><p>host网络和主机共享同一个网络，也就是host网络是和宿主机在一个网络，如：</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> run</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --rm</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> box2</span><span style="color:#D19A66;"> --network</span><span style="color:#98C379;"> host</span><span style="color:#98C379;"> busybox</span><span style="color:#98C379;"> /bin/sh</span><span style="color:#D19A66;"> -c</span><span style="color:#98C379;"> &quot;while true; do sleep 3600; done&quot;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><p>host网络可以直接把端口暴露给宿主机，不需要做端口映射。</p><h2 id="none网络" tabindex="-1">none网络 <a class="header-anchor" href="#none网络" aria-label="Permalink to &quot;none网络&quot;">​</a></h2><p>不会分配网络地址，主要是服务于一些编排工具，通过编排工具分配网络</p><div class="language-sh line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">sh</span><pre class="shiki one-dark-pro vp-code" style="background-color:#282c34;color:#abb2bf;" tabindex="0"><code><span class="line"><span style="color:#61AFEF;">docker</span><span style="color:#98C379;"> container</span><span style="color:#98C379;"> run</span><span style="color:#D19A66;"> -d</span><span style="color:#D19A66;"> --rm</span><span style="color:#D19A66;"> --name</span><span style="color:#98C379;"> box1</span><span style="color:#D19A66;"> --network</span><span style="color:#98C379;"> none</span><span style="color:#98C379;"> busybox</span><span style="color:#98C379;"> /bin/sh</span><span style="color:#D19A66;"> -c</span><span style="color:#98C379;"> &quot;while true; do sleep 3600; done&quot;</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br></div></div><h2 id="网络命名空间" tabindex="-1">网络命名空间 <a class="header-anchor" href="#网络命名空间" aria-label="Permalink to &quot;网络命名空间&quot;">​</a></h2><p><a href="https://dockertips.readthedocs.io/en/latest/single-host-network/network-namespace.html" target="_blank" rel="noreferrer">麦兜搞IT</a></p></div></div></main><footer class="VPDocFooter" data-v-39a288b8 data-v-e257564d><!--[--><!--]--><div class="edit-info" data-v-e257564d><!----><div class="last-updated" data-v-e257564d><p class="VPLastUpdated" data-v-e257564d data-v-e98dd255>最后更新: <time datetime="2024-12-30T14:20:32.000Z" data-v-e98dd255></time></p></div></div><nav class="prev-next" aria-labelledby="doc-footer-aria-label" data-v-e257564d><span class="visually-hidden" id="doc-footer-aria-label" data-v-e257564d>Pager</span><div class="pager" data-v-e257564d><a class="VPLink link pager-link prev" href="/docker/multi-architecture.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>上一篇</span><span class="title" data-v-e257564d>Docker 的多架构支持</span><!--]--></a></div><div class="pager" data-v-e257564d><a class="VPLink link pager-link next" href="/docker/security.html" data-v-e257564d><!--[--><span class="desc" data-v-e257564d>下一篇</span><span class="title" data-v-e257564d>安全</span><!--]--></a></div></nav></footer><!--[--><!--]--></div></div></div><!--[--><!--]--></div></div><!----><!--[--><!--]--></div></div>
    
    
  </body>
</html>